package se.webbzon.boltzmann.minigame.effect;

import java.awt.Image;

import se.webbzon.boltzmann.minigame.sandbox.SandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxIcon;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxItem;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;
import se.webbzon.oschi01.worldobject.WorldObject;

public class SandBoxSpark implements SandBoxItem {
	
	// The 2-dimensional depth of the rain
	private static final int depth = 2;
	
	// The maximal duration of a spark
	private static final int minDuration = 10, maxDuration = 20;
	
	// The image of the spark
	private final Image image;
	
	// The sand box to which this rain is attached
	private SandBox box;
	
	// The position of the spark
	private double x, y;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public SandBoxSpark(int index, double x, double y) {
		image = Resources.getImageSet("minigame_spark").get(index);
		this.x = x;
		this.y = y;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void onAttach(SandBox box) {
		box.removeItem(this);
	}

	@Override public void onDetach(SandBox box) {
		for (int i = 0; i < 30; i++) {
			Spark spark = new Spark();
			spark.setPosition(getX(), getY());
			box.addItem(spark);
		}
	}

	@Override public double getX() {
		return x;
	}

	@Override public double getY() {
		return y;
	}

	@Override public double getWidth() {
		return 0;
	}

	@Override public double getHeight() {
		return 0;
	}

	@Override public boolean draggable() {
		return false;
	}

	@Override public boolean droppable() {
		return false;
	}

	@Override public void setPosition(double x, double y) {
		this.x = x;
		this.y = y;
	}

	@Override public SandBoxIcon createIcon() {
		return null;
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class Spark extends WorldObject implements SandBoxItem {
		
		private int remaining;
		private double x, y, xv, yv;
		
		public Spark() {
			super(WorldObject.onStep + WorldObject.onPostDraw, depth);
			remaining = (int) (minDuration + Math.random() * (maxDuration - minDuration));
			xv = - 3 + 6 * Math.random();
			yv = - 3 + 6 * Math.random();
		}

		@Override public void onAttach(SandBox box) {
			box.getWorld().createInstance(this);
		}

		@Override public void onDetach(SandBox box) {
			box.getWorld().destroyInstance(this);
		}

		@Override public SandBoxIcon createIcon() {
			return null;
		}

		@Override public void onStep(WorldArea world) {
			setPosition(getX() + xv, getY() + yv);
			if (remaining > 0) {
				remaining--;
			} else if (remaining == 0) {
				if (box == null)
					world.destroyInstance(this);
				else
					box.removeItem(this);
			}
		}
		
		@Override public void onPostDraw(WorldArea world, WorldCameraPane hud) {
			hud.drawImage(image, (int) getX(), (int) getY());
		}

		@Override public boolean draggable() {
			return false;
		}

		@Override public boolean droppable() {
			return false;
		}

		@Override public void setPosition(double x, double y) {
			this.x = x;
			this.y = y;
		}

		@Override public double getX() {
			return x;
		}

		@Override public double getY() {
			return y;
		}

		@Override public double getWidth() {
			return 0;
		}

		@Override public double getHeight() {
			return 0;
		}
		
	}

}
